package com.example.androidlearning.sy7;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.example.androidlearning.R;
import com.example.androidlearning.sy5.sy5Activity03;

public class Sy7Activity01 extends AppCompatActivity {

    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sy07);

        init();
        setListener();
    }


    private void init() {

        listView = findViewById(R.id.sy07_listview);

        String[] itemName = new String[]{"ComponentName跳转", "Inter-filter配置", "返回Home", "打开百度首页","显示联系人","拨打电话"};

        ArrayAdapter<String > arrayAdapter = new ArrayAdapter<>(this, R.layout.listview_arry_item, itemName);
        listView.setAdapter(arrayAdapter);


    }

    private void setListener() {

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent it = new Intent();
                switch (position){
                    case 0:   //componentName
                        ComponentName cn = new ComponentName(Sy7Activity01.this, sy5Activity03.class);
                        it.setComponent(cn);
                        break;
                    case 1: //在androidManifest.xml中设置<activity>的intent-filter
                        it.setAction("my_action");
                        it.addCategory("my_category");
                        break;
                    case 2:   //返回home
                         it.setAction(Intent.ACTION_MAIN);
                         it.addCategory(Intent.CATEGORY_HOME);
                         break;
                    case 3:  //打开百度
                        it.setAction(Intent.ACTION_VIEW);
                        it.setData(Uri.parse("http://www.baidu.com"));
                        break;
                    case 4:  //显示联系人
                        it.setAction(Intent.ACTION_VIEW);
                        it.setData(Contacts.People.CONTENT_URI);
                        break;
                    case 5:  //拨打电话
                        it.setAction(Intent.ACTION_DIAL);
                        it.setData(Uri.parse("tel:10086"));
                        break;
                }
                startActivity(it);

            }
        });

    }

}
